package cn.etgps.etgps.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.etgps.etgps.R;
import cn.etgps.etgps.base.BaseActivity;
import cn.etgps.etgps.blmanager.UserManager;
import cn.etgps.etgps.dialog.DialogCreater;
import cn.etgps.etgps.utils.Constant;
import cn.etgps.etgps.view.TitleBar;

/**
 * 密码修改
 */
public class UpdatePassWordActivity extends BaseActivity implements View.OnClickListener {
    private TitleBar mTitlebarView = null;
    private UserManager mUserManger = null;
    /**
     * 旧密码
     */
    private EditText mOldPwd = null;
    /**
     * 新密码
     */
    private EditText mNewPwd = null;
    /**
     * 确认密码
     */
    private EditText mAgainPwd = null;
    /**
     * 提交信息
     */
    private Button mCommit = null;
    /**
     * 修改密码线程
     */
    private UpdatePasswordTask updatePasswordTask = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        initView();
    }

    private void initView() {
        mTitlebarView = (TitleBar) findViewById(R.id.title_bar);
        mTitlebarView.initTitleBarInfo(getString(R.string.label_pass_word_change_pwde), "", R.drawable.icon_return, "",
                0, false);
        mTitlebarView.setMainClickListener(this, getString(R.string.btn_cancel), "");
        mUserManger = new UserManager(this);
        mOldPwd = (EditText) findViewById(R.id.inputOldPassword);
        mNewPwd = (EditText) findViewById(R.id.inputNewPassword);
        mAgainPwd = (EditText) findViewById(R.id.againInputPassword);
        mCommit = (Button) findViewById(R.id.commit);
        mCommit.setOnClickListener(this);
    }

    @Override
    public void clickButtonListener(String name) {
        if (name.equals(getString(R.string.btn_cancel))) {
            this.finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commit:
                String oldpwd = mOldPwd.getText().toString().trim();
                String newpwd = mNewPwd.getText().toString().trim();
                String againpwd = mAgainPwd.getText().toString().trim();
                if (checkInput(oldpwd, newpwd, againpwd)) {
                    //首先进行网络判断
                    if (mUserManger.checkNetwork(this)) {
                        if (updatePasswordTask != null && updatePasswordTask.getStatus() == AsyncTask.Status.RUNNING) {
                            updatePasswordTask.cancel(true);
                        }
                        updatePasswordTask = new UpdatePasswordTask();
                        updatePasswordTask.execute(mUserManger.getUserInfo()[0], oldpwd, newpwd,
                                mUserManger.getUserType());
                    } else {
                        mUserManger.showToastMessage(getString(R.string.toast_no_network), 1000);
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 检测是否符合要求
     */
    private boolean checkInput(String oldPassword, String newPassword, String againPassword) {
        if (oldPassword.equals("")) {
            mUserManger.showToastMessage(getString(R.string.toast_null_old_password), 1000);
            return false;
        } else if (!oldPassword.equals(mUserManger.getUserInfo()[1])) {
            mUserManger.showToastMessage(getString(R.string.toast_wrong_old_password), 1000);
            return false;
        } else if (newPassword.equals("")) {
            mUserManger.showToastMessage(getString(R.string.toast_null_new_password), 1000);
            return false;
        } else if (againPassword.equals("")) {
            mUserManger.showToastMessage(getString(R.string.toast_null_again_new_password), 1000);
            return false;
        } else if (!againPassword.equals(newPassword)) {
            mUserManger.showToastMessage(getString(R.string.toast_new_mismatch_password), 1000);
            return false;
        }
        return true;
    }

    /**
     * 开启修改密码请求线程
     */
    public class UpdatePasswordTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            showNormalDialog(DialogCreater.DIALOG_PROGRESS);
            return mUserManger.changePwd(params);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            dismissNormalDialog();
            if (s.equals(Constant.RETURN_RESULT_OK)) {
                mUserManger.showToastMessage(getString(R.string.toast_change_pwd_ok), 1000);
                //这里是否需要重新登录
                String[] ms = mUserManger.getUserInfo();
                String[] save = new String[5];
                save[0] = ms[0];
                save[1] = mUserManger.getUserID();
                save[2] = mUserManger.getUserType();
                save[3] = "";
                save[4] = ms[2];
                mUserManger.saveUserLoginBasicInfo(save);
                finish();
            } else if (s.equals(Constant.CHANGE_PWD_FAILED)) {
                mUserManger.showToastMessage(getString(R.string.toast_change_pwd_failed), 1000);
            } else {
                mUserManger.showToastMessage(s, 1000);
            }
        }
    }
}
